package cc.linker.steplink.crm.controller;


import cc.linker.steplink.crm.commons.model.OssProperties;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.service.acs.DynamicService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.response.ResponseException;
import cc.linker.steplink.crm.service.ILevelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/level")
public class LevelController {

    @Autowired
    private ILevelService levelService;

    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @Value("${casclient.clientHost}")
    private String imgUrl;

    @PostMapping("/list")
    public Object list(@RequestBody Map<String, Object> map, HttpServletRequest request) throws GlobalException {
        int pn = 1, pageSize = 10;
        Object pno = map.get("pn");
        if (pno instanceof Integer && !StringUtils.isEmpty(pno)) {
            pn = (int) pno;
        }
        Object pageSizeo = map.get("pageSize");
        if (pageSizeo instanceof Integer && !StringUtils.isEmpty(pageSizeo)) {
            pageSize = (int) pageSizeo;
        }
        PageHelper.startPage(pn, pageSize);
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        if (StringUtils.isEmpty(orgId)) {
            throw new GlobalException(ResponseCode.INFO_OBTAIN_FAIL);
        }
        map.put("orgId", orgId);
        List<Map<String, Object>> list = levelService.listLevels(map);

        return new PageInfo<>(list);
    }

    @PostMapping("/add")
    public void add(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        if (StringUtils.isEmpty(orgId)) {
            throw new GlobalException(ResponseCode.INFO_OBTAIN_FAIL);
        }
        map.put("orgId", orgId);
        int lastOrder = levelService.lastOrder(orgId) + 1;
        int lastScore = levelService.getLastScore(orgId);
        if (lastScore >= Integer.parseInt(map.get("levelScore").toString())) {
            throw new GlobalException(new ResponseException(400, "添加失败，所需成长值必须大于最高等级成长值"));
        }
        map.put("levelOrder", lastOrder);
        map.put("levelId", UUID.randomUUID().toString());
        Integer giftStatus = 0;
        Integer levelGifts = 0;
        Integer levelGrowth = 0;
        if (!StringUtils.isEmpty(map.get("giftStatus"))) {
            giftStatus = Integer.valueOf(map.get("giftStatus").toString());
        }
        if (!StringUtils.isEmpty(map.get("levelGifts"))) {
            levelGifts = Integer.valueOf(map.get("levelGifts").toString());
        }
        if (!StringUtils.isEmpty(map.get("levelGrowth"))) {
            levelGrowth = Integer.valueOf(map.get("levelGrowth").toString());
        }
        map.put("levelGifts", levelGifts);
        map.put("levelGrowth", levelGrowth);
        map.put("giftStatus", giftStatus);
        levelService.addLevel(map);
    }

    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        if (StringUtils.isEmpty(orgId)) {
            throw new GlobalException(ResponseCode.INFO_OBTAIN_FAIL);
        }
        if (null == map.get("levelId") || "".equals(map.get("levelId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
        String levelId = map.get("levelId").toString();
        String lastLevelId = levelService.getLastLevelId(orgId);
        if (!levelId.equals(lastLevelId)) {
            throw new GlobalException(new ResponseException(400, "删除失败，只能从最高等级开始删除"));
        }
        //获取当前等级
        levelService.delLevel(levelId, orgId);
    }


    @PostMapping("/getinfo")
    public Object getLevelInfo(HttpServletRequest request, @RequestBody Map<String, Object> map) throws Exception {
        if (null == map.get("levelId") || "".equals(map.get("levelId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
        String levelId = map.get("levelId").toString();
        HashMap<String, Object> levelInfo = levelService.getLevelInfo(levelId,request);
        if (!(levelInfo instanceof Map)) {
            throw new GlobalException(new ResponseException(400, "参数错误"));
        }

        return levelInfo;
    }

    @PostMapping("/edit")
    public void edit(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        dynamicService.recording(request, 14,"调整了会员等级");
        if (null == map.get("levelId") || "".equals(map.get("levelId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }

        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        if (StringUtils.isEmpty(orgId)) {
            throw new GlobalException(ResponseCode.INFO_OBTAIN_FAIL);
        }

        HashMap<String, Object> nowInfo = levelService.getLevelInfo(map.get("levelId").toString(),request);
        int nowLevel = Integer.parseInt(nowInfo.get("levelOrder").toString());
        Map<String, Object> smap = new HashMap();
        smap.put("orgId", orgId);
        smap.put("levelOrder", nowLevel);

        HashMap<String, Object> preInfo = levelService.getPreLevelInfo(smap);
        HashMap<String, Object> nextInfo = levelService.getNextLevelInfo(smap);
        if (null != preInfo) {
            if (Integer.parseInt(map.get("levelScore").toString()) <= Integer.parseInt(preInfo.get("levelScore").toString())) {
                throw new GlobalException(new ResponseException(400, "修改失败，所需成长值必须大于前一等级成长值"));
            }
        }
        if (null != nextInfo) {
            if (Integer.parseInt(map.get("levelScore").toString()) >= Integer.parseInt(nextInfo.get("levelScore").toString())) {
                throw new GlobalException(new ResponseException(400, "修改失败，所需成长值必须小于下一等级成长值"));
            }
        }

        if (StringUtils.isEmpty(map.get("levelGifts")) || "".equals(map.get("levelGifts"))) {
            map.put("levelGifts", 0);
        }
        map.put("giftStatus", StringUtils.isEmpty(map.get("giftStatus")) ? "" : map.get("giftStatus").toString());
        levelService.editLevel(map,nowInfo);
    }
}
